package cn.iocoder.yudao.module.shop.service.crosstenant;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.shop.controller.admin.crosstenant.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.crosstenant.ShopCrossTenantDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.shop.convert.crosstenant.ShopCrossTenantConvert;
import cn.iocoder.yudao.module.shop.dal.mysql.crosstenant.ShopCrossTenantMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 跨店铺 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class ShopCrossTenantServiceImpl implements ShopCrossTenantService {

    @Resource
    private ShopCrossTenantMapper crossTenantMapper;

    @Override
    public Long createCrossTenant(ShopCrossTenantCreateReqVO createReqVO) {
        // 插入
        ShopCrossTenantDO crossTenant = ShopCrossTenantConvert.INSTANCE.convert(createReqVO);
        crossTenantMapper.insert(crossTenant);
        // 返回
        return crossTenant.getId();
    }

    @Override
    public void updateCrossTenant(ShopCrossTenantUpdateReqVO updateReqVO) {
        // 校验存在
        validateCrossTenantExists(updateReqVO.getId());
        // 更新
        ShopCrossTenantDO updateObj = ShopCrossTenantConvert.INSTANCE.convert(updateReqVO);
        crossTenantMapper.updateById(updateObj);
    }

    @Override
    public void deleteCrossTenant(Long id) {
        // 校验存在
        validateCrossTenantExists(id);
        // 删除
        crossTenantMapper.deleteById(id);
    }

    private void validateCrossTenantExists(Long id) {
        if (crossTenantMapper.selectById(id) == null) {
            throw exception(CROSS_TENANT_NOT_EXISTS);
        }
    }

    @Override
    public ShopCrossTenantDO getCrossTenant(Long id) {
        return crossTenantMapper.selectById(id);
    }

    @Override
    public List<ShopCrossTenantDO> getCrossTenantByTenantId(Long tenantId){
        return crossTenantMapper.selectList(ShopCrossTenantDO::getTenantId, tenantId);
    }

    @Override
    public List<ShopCrossTenantDO> getCrossTenantList(Long tenantId){
        return  crossTenantMapper.selectList(ShopCrossTenantDO::getTenantId, tenantId);
    }

    @Override
    public List<ShopCrossTenantDO> getCrossTenantList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return crossTenantMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ShopCrossTenantDO> getCrossTenantPage(ShopCrossTenantPageReqVO pageReqVO) {
        return crossTenantMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShopCrossTenantDO> getCrossTenantList(ShopCrossTenantExportReqVO exportReqVO) {
        return crossTenantMapper.selectList(exportReqVO);
    }

}
